package com.haha.gulimall.ware.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haha.common.utils.PageUtils;
import com.haha.common.utils.Query;

import com.haha.gulimall.ware.dao.PurchaseDetailDao;
import com.haha.gulimall.ware.entity.PurchaseDetailEntity;
import com.haha.gulimall.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    /**
     * key: 10
     * status: 1
     * wareId: 2
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> qw = new QueryWrapper<>();
        String key = (String) params.get("key");
        String wareId = (String) params.get("wareId");
        String status = (String) params.get("status");
        qw.eq(StringUtils.isNotEmpty(wareId),"ware_id",wareId)
                .eq(StringUtils.isNotEmpty(status),"status",status);
        if(StringUtils.isNotEmpty(key)){
            qw.and(wrapper->{
                wrapper.eq("id",key)
                        .or().eq("purchase_id",key)
                        .or().eq("sku_id",key);
            });
        }



        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                qw
        );

        return new PageUtils(page);
    }

    @Override
    public List<PurchaseDetailEntity> getDetailByPurchaseId(Long id) {
        List<PurchaseDetailEntity> purchase_id = this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", id));
        return purchase_id;

    }

}